package com.note.app.service.impl;

import com.note.app.config.RabbitMQConfig;
import com.note.app.dto.log.RequestLogDTO;
import com.note.app.service.LogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

/**
 * 日志服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class LogServiceImpl implements LogService {
    
    private final RabbitTemplate rabbitTemplate;
    
    @Override
    public void recordRequestLog(RequestLogDTO logDTO) {
        try {
            // 发送日志到RabbitMQ
            rabbitTemplate.convertAndSend(
                    RabbitMQConfig.LOG_EXCHANGE,
                    RabbitMQConfig.REQUEST_LOG_ROUTING_KEY,
                    logDTO
            );
            
            if (log.isDebugEnabled()) {
                log.debug("发送日志到消息队列成功: requestId={}, uri={}", logDTO.getRequestId(), logDTO.getRequestUri());
            }
        } catch (Exception e) {
            // 出现异常不影响正常业务流程，只记录错误日志
            log.error("发送日志到消息队列失败: {}", e.getMessage(), e);
        }
    }
} 